/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
(c) Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 2009-3-19
Author  : 
Company : 
Comments: 


Chip type           : ATmega16
Program type        : Application
Clock frequency     : 7.372800 MHz
Memory model        : Small
External RAM size   : 0
Data Stack size     : 256
 *****************************************************/

#include "pcf8563.h"
#include "i2cmaster.h"

uint8_t rtcInit(void){
    uint8_t ret = 0;
    ret = i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_check(ret);

    ret = i2c_write(0x00);//reg addr
    i2c_check(ret);
    i2c_write(0x00);//C1
    i2c_write(0x00);//C2

    i2c_write(0x00);//S
    i2c_write(0x00);//M
    i2c_write(0x00);//H
    i2c_write(0x01);//D
    i2c_write(0x00);//WD
    i2c_write(0x01);//M
    i2c_write(0x00);//Y

    i2c_write((1<<RTC_AE));//AM
    i2c_write((1<<RTC_AE));//AH
    i2c_write((1<<RTC_AE)|(0x01));//AD
    i2c_write((1<<RTC_AE));//AWD

    i2c_write(0x00);//COC
    i2c_write(0x03);//TC
    i2c_write(0x00);//T

    i2c_stop();
    return ret;
}

inline void rtcSetAlarmIE(RTC* rtime) {
    rtime.control.con2 |= (1<<RTC_AIE);
}

inline void rtcClrAlarmIE(RTC* rtime) {
    rtime.control.con2 &= !(1<<RTC_AIE);
}

inline void rtcSetTimerIE(RTC* rtime) {
    rtime.control.con2 |= (1<<RTC_TIE);
}

inline void rtcClrTimerIE(RTC* rtime) {
    rtime.control.con2 &= !(1<<RTC_TIE);
}

inline void rtcTimerEn(RTC* rtime) {
    rtime.control.timer = 0xFF;//1/60 HZ
}

inline void rtcTimerDis(RTC* rtime) {
    rtime.control.timer = 0x03;
}

void rtcSetControl(RTC* rtime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_CONTROL_STATUS2);
    i2c_write(rtime.control.con2);
    i2c_stop();
}

void rtcSetupTimer(RTC* rtime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_CONTROL_STATUS2);
    i2c_write(*rtime.control.con2);
    i2c_rep_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_TIMER_CONTROL);
    i2c_write(*rtime.control.timer);
    i2c_write(*rtime.timer);
    i2c_stop();
}

void rtcSetTimer(RTC* rtime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_TIMER);
    i2c_write(*rtime.timer);
    i2c_stop();
}

void rtcSetMinAlarm(RTC* rtime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_CONTROL_STATUS2);
    i2c_write(*rtime.control.con2);
    i2c_rep_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_MINUTE_ALARM);
    i2c_write(*rtime.alarm.min);
    i2c_stop();
}

void rtcReadTime(RTC* pTime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_SECONDS);
    i2c_rep_start(RTC_ADDR+I2C_READ);

    *pTime.time.sec   = i2c_read(ACK);
    *pTime.time.min   = i2c_read(ACK);
    *pTime.time.hour  = i2c_read(ACK);
    *pTime.time.day   = i2c_read(ACK);
    *pTime.time.wday  = i2c_read(ACK);
    *pTime.time.mon   = i2c_read(ACK);
    *pTime.time.year  = i2c_read(NACK);

    i2c_stop();
}

void rtcReadAlarm(RTC* pTime) {
    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_MINUTE_ALARM); 
    i2c_rep_start(RTC_ADDR+I2C_READ);
    
    *pTime.alarm.min  = i2c_read(ACK);
    *pTime.alarm.hour = i2c_read(ACK);
    *pTime.alarm.day  = i2c_read(ACK);
    *pTime.alarm.wday = i2c_read(NACK);
    
    i2c_stop();
}

void rtcWriteReg(uint8_t addr,uint8_t val){
    uint8_t ret;
    ret = i2c_start(RTC_ADDR + I2C_WRITE);
    i2c_check(ret);

    i2c_write(addr);
    i2c_write(val);
    i2c_stop();
}

uint8_t rtcReadReg(uint8_t addr){
    uint8_t ret;

    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(addr);
    i2c_rep_start(RTC_ADDR+I2C_READ);
    ret = i2c_read(NACK);
    i2c_stop();

    return ret;
}

void rtcSetClock(RTC* pTime) {
    i2c_start(RTC_ADDR + I2C_WRITE);
    i2c_write(RTC_SECONDS);
    i2c_write(*pTime.time.sec);
    i2c_write(*pTime.time.min);
    i2c_write(*pTime.time.hour);
    i2c_write(*pTime.time.day);
    i2c_write(*pTime.time.wday);
    i2c_write(*pTime.time.mon);//century bit is always zero
    i2c_write(*pTime.time.year);
    i2c_stop();
}

uint8_t rtcReadIntegrity(void){
    uint8_t ret;

    i2c_start(RTC_ADDR+I2C_WRITE);
    i2c_write(RTC_SECONDS);
    i2c_rep_start(RTC_ADDR+I2C_READ);
    ret = i2c_read(NACK);
    i2c_stop();

    return ret & 0x80;
}

uint8_t rtc8Bit2BCD2(uint8_t Bin){ //2 digits (max 59)
    uint8_t BCD;

    BCD = Bin%10;
    Bin -= BCD;
    BCD += Bin/10;

    return BCD;
}

uint8_t rtc2BCD2Bit8(uint8_t BCD){
    uint8_t Bin;
    uint8_t temp;

    Bin = BCD & 0x0F;
    temp = (BCD & 0xF0)>>4;
    Bin += temp*10;

    return Bin;
}

